package com.pan.behavioral.template.read.impl;

import com.pan.behavioral.template.read.ReadFile;

import java.io.*;

public abstract class BasicReadFile implements ReadFile {

    protected InputStream inputStream;


    protected String path;
    public BasicReadFile(String path){
        this.path = path;
    }
    @Override
    public void openFile() {
        File file = new File(path);
        if (!file.exists()){
            throw new RuntimeException("No Such File: "+path);
        }
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public abstract boolean hasNext() ;

    @Override
    public abstract String read() ;

    @Override
    public void end() {
        try {
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
